import moment from 'moment/moment';

// 获取url中的query项
export function getQueryString(name) {
  const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`, 'i');
  const r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}

// 获取url中的hash项
export function getHashString(name) {
  const reg = new RegExp(`([?|&])${name}=([^&]*)(&|$)`, 'i');
  const r = window.location.hash.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}

export function getPhoneType() {
  const u = navigator.appVersion;
  const android = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
  const iPhone = u.indexOf('iPhone') > -1;
  if (android) {
    return 'Android';
  }
  if (iPhone) {
    return 'iOS';
  }
  return 'Android';
}

/**
 *
 * 生成query字符串
 * @param obj
 * @returns {string}
 *
 */
export function genQuery(obj) {
  let index = 0;
  let query = '';
  Object.keys(obj).forEach((name) => {
    if (obj[name] !== undefined && obj[name] !== null) {
      if (index !== 0) {
        query += '&';
      }
      const value = obj[name];
      query += `${name}=${value}`;
      index += 1;
    }
  });
  return query;
}

/**
 * 获取当前浏览器高度 - 指定值的高度
 * @param height
 * @returns {number}
 */
export function getCurrentHeight(height) {
  const rem = document.documentElement.clientWidth / 10;
  return (document.documentElement.clientHeight / rem) - (height / 75);
}
const TIME_FORMAT = 'YYYY/MM/DD';
export function formatUtcTime(timeSecond, dstFormat) {
  const outformat = dstFormat || TIME_FORMAT;
  return moment.utc(timeSecond).format(outformat);
}

/**
 * 生成UUid
 * @returns {string}
 */
/* eslint-disable */
export function generateUUID() {
  let d = new Date().getTime();
  const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    let r = (d + Math.random() * 16) % 16 | 0;
    d = Math.floor(d/16);
    return (c=='x' ? r : (r&0x7|0x8)).toString(16);
  });
  return uuid;
}


// 验证手机号
export function checkMobilePhone(linkPhone) {
  const regphone = /^1[0-9]{10}$/;
  if (!regphone.test(linkPhone.trim())) {
    return false;
  }
  return true;
}
